package com.xindacloud.sms.adapter.utils;

import com.google.i18n.phonenumbers.PhoneNumberToCarrierMapper;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder;
import com.xindacloud.sms.adapter.bo.CheckPhoneNumberBO;
import com.xindacloud.sms.adapter.exception.ServiceException;

import java.util.Locale;
import java.util.Objects;
import java.util.Set;

public class PhoneUtil {

    private static PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();

    private static PhoneNumberToCarrierMapper carrierMapper = PhoneNumberToCarrierMapper.getInstance();

    private static PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();

    /**
     * 根据国家代码和手机号  判断手机号是否有效
     *
     * @param phoneNumber
     * @param countryCode
     * @return
     */
    public static boolean checkPhoneNumber(String phoneNumber, Integer countryCode) {

        long phone = Long.parseLong(phoneNumber);

        Phonenumber.PhoneNumber pn = new Phonenumber.PhoneNumber();
        pn.setCountryCode(countryCode);
        pn.setNationalNumber(phone);

        return phoneNumberUtil.isValidNumber(pn);

    }

    /**
     * 根据国家代码和手机号  判断手机运营商
     *
     * @param phoneNumber
     * @param countryCode
     * @return
     */
    public static String getCarrier(String phoneNumber, Integer countryCode) {
        long phone = Long.parseLong(phoneNumber);
        Phonenumber.PhoneNumber pn = new Phonenumber.PhoneNumber();
        pn.setCountryCode(countryCode);
        pn.setNationalNumber(phone);
        //返回结果只有英文，自己转成成中文
        return carrierMapper.getNameForNumber(pn, Locale.ENGLISH);
    }


    /**
     * @param phoneNumber
     * @param countryCode
     * @return 参数
     * @Description: 根据国家代码和手机号  手机归属地
     * @date 2017-4-26 上午11:33:18
     */
    public static String getGeo(String phoneNumber, Integer countryCode) {

        long phone = Long.parseLong(phoneNumber);

        Phonenumber.PhoneNumber pn = new Phonenumber.PhoneNumber();
        pn.setCountryCode(countryCode);
        pn.setNationalNumber(phone);

        return geocoder.getDescriptionForNumber(pn, Locale.US);
    }

    public static CheckPhoneNumberBO getPoneNumber(String phoneNumber) {

        try {
            final Set<String> supportedRegions = phoneNumberUtil.getSupportedRegions();
            for (String region : supportedRegions) {
                final Phonenumber.PhoneNumber parse = phoneNumberUtil.parse(phoneNumber, region);
                String combinedNumber;
                if (String.valueOf(parse.getNationalNumber()).startsWith(String.valueOf(parse.getCountryCode()))) {
                    combinedNumber = phoneNumber;
                } else {
                    combinedNumber = String.valueOf(parse.getCountryCode()).concat(String.valueOf(parse.getNationalNumber()));
                }
//                final String combinedNumber = String.valueOf(parse.getCountryCode()).concat(String.valueOf(parse.getNationalNumber()));
                final String countryCode = phoneNumberUtil.getRegionCodeForCountryCode(parse.getCountryCode());
                if (Objects.equals(combinedNumber, phoneNumber) && Objects.equals(countryCode, region)) {
                    return CheckPhoneNumberBO.builder()
                            .countryPrefix(parse.getCountryCode())
                            .nationalNumber(parse.getNationalNumber())
                            .region(region)
                            .build();
                }
            }
        } catch (Exception e) {
            throw new ServiceException("号码不合法.", UnionResultHelp.StatusEnum.ParamsFail.code());
        }
        return null;
    }
}
